<?php

class CategoryAction extends PublicAction
{
    protected $categoryModel;
    protected $model;

    function _initialize()
    {
        parent::_initialize();

        foreach ((array)$this->Model as $rw) {
            if ($rw['type'] == 1) {
                $model[$rw['id']] = $rw;
            }
        }

        $this->assign('model', $model);
        $this->categoryModel = D('Category');
    }

    //栏目列表
    public function index()
    {
        $models = getCache('Model');
        $this->assign('models', $models);

        $condition = array();
        $condition['lang'] = LANG_ID;

        $categories = D('Category')->relation('Model')->where($condition)->order('listorder desc,id asc')->select();

        if (!empty($categories)) {

            import('@.ORG.Tree');
            $tree = new Tree();
            $data_list = $tree->toFormatTree($categories, 'catname', 'id', 'parentid');

            $this->assign('list', $data_list);
        }

        //记录当前位置
        cookie('__forward__', $_SERVER['REQUEST_URI']);

        $this->display();
    }

    public function add()
    {
        if (IS_POST) {
            $catid = $this->categoryModel->addCategory($_POST);
            if ($catid) {
                if ($_POST['type'] == 1) {
                    $content = I('content', '', 'trim');
                    $wap_content = I('wap_content', '', 'trim');
                    $data = array(
                        'catid'       => $catid,
                        'content'     => $content,
                        'wap_content' => $wap_content
                    );
                    M('Page')->add($data);
                }
                $this->success('添加成功！', U('category/index'));
            } else {
                $this->error($this->categoryModel->getError());
            }
        } else {
            $parentid = I('parentid', 0, 'intval');
            $type = I('type');
            $parentCatData = [
                'type'    => 0,
                'modelid' => 0
            ];
            $parentCatData['ismenu'] = 1;
            if ($this->Categorys[$parentid]) {
                $parentCatData['modelid'] = $this->Categorys[$parentid]['modelid'];
                $parentCatData['type'] = $this->Categorys[$parentid]['type'];
            }

            $this->assign('parentCatData', $parentCatData);

            foreach ($this->Categorys as $r) {
                $r['disabled'] = '';
                if ($r['type'] == 0 && $r['modelid'] != $parentCatData['modelid']) {
                    $r['disabled'] = ' disabled';
                }
                $array[] = $r;
            }
            import('@.ORG.Tree');
            $tree = new Tree($array);
            $select_categorys = $tree->get_tree(0, $parentid, 'catname');
            $this->assign('select_categorys', $select_categorys);

            //角色组
            $this->assign("Role_group", M("AuthGroup")->order(array("id" => "ASC"))->select());

            if ($type == 1) {
                $this->display('add_page');
            } elseif ($type == 2) {
                $this->display('add_link');
            } else {
                $this->display();
            }
        }
    }

    //编辑
    public function edit()
    {
        if (IS_POST) {
            $status = $this->categoryModel->editCategory($_POST);
            if ($_POST['type'] == 1) {
                $content = I('content', '', 'trim');
                $wap_content = I('wap_content', '', 'trim');
                $data = array(
                    'content'     => $content,
                    'wap_content' => $wap_content
                );
                M('Page')->where('catid=' . $_POST['id'])->save($data);
            }
            if ($status) {
                $this->success('修改成功!');
            } else {
                $error = $this->categoryModel->getError();
                $this->error($error ? $error : '栏目修改失败！');
            }
        } else {
            $id = I('id', 0, 'intval');

            //读取数据
            $data = M('Category')->find($id);

            if (empty($id) || empty($data)) {
                $this->error('缺少必要的参数!');
            }

            //扩展配置
            $data['setting'] = unserialize($data['setting']);
            //栏目扩展字段
            $data['extend'] = $data['setting']['extend'];

            $this->assign('data', $data);

            //获取路由方法
            foreach ((array)$this->Urlrule as $key => $r) {
                if ($r['ishtml']) {
                    $Urlrule[$key] = $r;
                }
            }

            $this->assign('Urlrule', $Urlrule);

            import('@.ORG.Tree');
            $result = $this->Categorys;

            $parentid = intval($data['parentid']);
            foreach ($result as $r) {
                $r['selected'] = $r['id'] == $parentid ? 'selected' : '';
                $r['disabled'] = '';
                if ($r['type'] == 0 && $r['modelid'] != $data['modelid']) {
                    $r['disabled'] = ' disabled';
                }
                if ($r['id'] == $data['id']) {
                    $r['disabled'] = ' disabled';
                }

                $array[] = $r;
            }

            $tree = new Tree($array);
            $select_categories = $tree->get_tree(0, $parentid, 'catname');
            $this->assign('select_categorys', $select_categories);

            //栏目扩展字段
            $this->assign('extendList', $this->categoryModel->getExtendField($id));


            if ($data['type'] == 1) {
                $this->assign('page', M('Page')->where(['catid' => $id])->find());
                $this->display('edit_page');
            } elseif ($data['type'] == 2) {
                $this->display('edit_link');
            } else {
                $this->display();
            }
        }
    }

    //栏目排序
    public function listorder()
    {
        $model = M('Category');
        $ids = $_POST['listorders'];

        foreach ($ids as $key => $r) {
            $data['listorder'] = $r;
            $model->where('id=' . $key)->save($data);
        }

        saveCache('Category');
        $this->success('排序成功!');
    }

    //删除栏目
    public function delete()
    {
        $catid = I("get.id", "", "intval");

        if (!$catid) {
            $this->error("请指定需要删除的栏目！");
        }

        if (false == $this->categoryModel->deleteCatid($catid)) {
            $this->error("栏目删除失败，错误原因可能是栏目下存在信息，无法删除！");
        }
        $this->success("栏目删除成功！");
    }
}